package net.yla.smartbus.module.ba.vehicletype.serice.impl;

import com.github.pagehelper.PageHelper;
import net.yla.smartbus.core.base.PageBean;
import net.yla.smartbus.module.ba.vehicletype.detachment.VehicleTypeDetachment;
import net.yla.smartbus.module.ba.vehicletype.dto.VehicleTypeDto;
import net.yla.smartbus.module.ba.vehicletype.entity.BaVehicleModelsDo;
import net.yla.smartbus.module.ba.vehicletype.form.VehicleTypeForm;
import net.yla.smartbus.module.ba.vehicletype.mapper.VehicleTypeMapper;
import net.yla.smartbus.module.ba.vehicletype.query.VehicleTypeQuery;
import net.yla.smartbus.module.ba.vehicletype.serice.VehicleTypeService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 默苍离
 * @date 2021/6/23 9:50
 * <p>
 * ******************************************
 * @annotation:
 */
@Service
public class VehicleTypeServiceImpl implements VehicleTypeService {

    private final VehicleTypeMapper mapper;

    public VehicleTypeServiceImpl(VehicleTypeMapper mapper) {
        this.mapper = mapper;
    }


    /**
     * 获取所有车型
     *
     * @param query
     * @return
     */
    @Override
    public List<VehicleTypeDto> getList(VehicleTypeQuery query) {

        List<BaVehicleModelsDo> vehicleTypeList = mapper.getList(query);

        List<VehicleTypeDto> vehicleTypeDtoList = new ArrayList<>();

        for (int i = 0; i < vehicleTypeList.size(); i++) {
            BaVehicleModelsDo vehicleType = vehicleTypeList.get(i);
            VehicleTypeDto vehicleTypeDto = VehicleTypeDetachment.vehicleTypeDoToTto(vehicleType);
            vehicleTypeDtoList.add(vehicleTypeDto);
        }

        return vehicleTypeDtoList;

    }


    /**
     * 获取所有车型数量
     *
     * @param query
     * @return
     */
    @Override
    public Integer getCount(VehicleTypeQuery query) {
        Integer count = mapper.getCount(query);
        return count;
    }


    /**
     * 分页所有车型
     *
     * @param query
     * @return
     */
    @Override
    public PageBean<VehicleTypeDto> getPage(VehicleTypeQuery query) {

        Integer count = getCount(query);

        PageHelper.startPage(query.getPageNum(), query.getPageSize(), "lastModifyTime desc");
        List<VehicleTypeDto> list = getList(query);

        PageBean<VehicleTypeDto> pageBean = new PageBean<>(list, query.getPageNum(), query.getPageSize(), count);

        return pageBean;
    }


    /**
     * 根据id进行删除
     *
     * @param ids
     * @return
     */
    @Override
    public Integer delete(Integer[] ids) {
        Integer delete = mapper.delete(ids);
        return delete;
    }


    /**
     * 新增车辆类型
     *
     * @param form
     * @return
     */
    @Override
    public Integer insert(VehicleTypeForm form) {

        Integer insert = mapper.insert(form);
        return insert;
    }


    /**
     * 根据id获取 车型
     *
     * @param id
     * @return
     */
    @Override
    public VehicleTypeDto getVehicleTypeById(Integer id) {
        BaVehicleModelsDo vehicleType = mapper.getVehicleTypeById(id);
        if (vehicleType != null) {
            VehicleTypeDto vehicleTypeDto = VehicleTypeDetachment.vehicleTypeDoToTto(vehicleType);
            return vehicleTypeDto;
        }
        return null;
    }


    /**
     * 跟新数据
     *
     * @param form
     * @return
     */
    @Override
    public Integer update(VehicleTypeForm form) {
        Integer update = mapper.update(form);
        return update;
    }


}
